<?php
/**
 * ==============================================
 * Copy right 2015-2016 http://wei.meididi88.com
 * ----------------------------------------------
 * This is not a free software, without any authorization is not allowed to use and spread.
 * ==============================================
 * @author: hy
 * @date: 2015-9-10
 * @version: v1.0.0
 */
namespace Common\Model;

use Think\Model;

/**
 * 订单Model模型类
 */
class OrderModel extends Model{
	
	protected $_valid = array(
		array('order_number','require','服务名必须填写！'),
		array('service_id','require','服务时长必须填写！'),
		array('beauty_id','require', '功效必须填写！'),
		array('beautician_id','require','服务简介必须填写！'),
		array('user_id','require', '适用人群必须填写！'),
		array('order_time','require', '注意事项必须填写！'),
		array('expire_time','require', '价格必须填写！'),
		array('created','require', '价格必须填写！'),
		array('service_name','require', '功效必须填写！'),
		array('price','require','服务简介必须填写！'),
		array('service_address','require', '适用人群必须填写！'),
	);
	
	/**
	 * @var number
	 */
	public $offset;
	
	/**
	 * @var number
	 */
	public $limit;
	
	/**
	 * @var string
	 */
	public $order = 'order_time';
	
	public $status;
	
	/**
	 * @param number $id
	 */
	public function clearCache($id) {
		$cacheKey = $this->getCacheKey($id);
		S($cacheKey, null);
	}
	
	public function checkCanOrder($beauticianId, $orderTimes, $selectStart, $selectEnd) {
		$sql = "SELECT * FROM `".C('DB_PREFIX')."order` 
				WHERE `beautician_id` = $beauticianId 
				AND  order_time >= $selectStart 
				AND (status=1 OR status=3 OR status=4 OR (status=2 AND expire_time>".time().")) 
				AND order_time < $selectEnd 
				AND ((order_time+(CEIL(service_time/60))*3600) >= $orderTimes)";
		
		$sql1 = "SELECT * FROM `".C('DB_PREFIX')."order`
		WHERE `beautician_id` = $beauticianId
		AND  order_time >= $selectStart
		AND (status=1 OR status=3 OR status=4 OR (status=2 AND expire_time>".time()."))
		AND order_time < $selectEnd
		AND order_time=$orderTimes ";
		
		$sql2 = "SELECT * FROM `".C('DB_PREFIX')."order`
		WHERE `beautician_id` = $beauticianId
		AND  order_time >= $selectStart
		AND (status=1 OR status=3 OR status=4 OR (status=2 AND expire_time>".time()."))
		AND order_time < $selectEnd
		AND order_time>$orderTimes AND order_time < $selectEnd)";
		
		return $this->query($sql . 'uniou '.$sql1 .' uniou '.$sql2);
	}
	
	/**
	 * 组装美容师查询条件
	 * 
	 * @param number $id
	 * @return multitype:unknown string number NULL 
	 */
	public function getBeauticianMultiSelectWhere($id) {
		$where = array('beautician_id' => $id);
		if ($this->status !== false) {
			if ($this->status == 1) {
				$where [] = 'status='.$this->status .' or status=3';
			} else {
				$where ['status'] = $this->status;
			}
		}
		
		return $where;
	}
	
	/**
	 * 预约服务
	 *
	 * @param array $data	订单数据
	 * @return boolean
	 */
	public function createOrder($data) {
		if (!$this->create($data, 1)) {
			$this->rollback();
			return false;
		} else {
			$orderId = 0;
			if (!($orderId = $this->add())) {
				$this->rollback();
				return false;
			}
		}
	
		return $orderId;
	}
	
	public function getCacheKey($id) {
		return 'Order_'.$id;
	}
	
	/**
	 * 获取用户预约列表
	 * @param number $userId	用户编号
	 * @return boolean|array
	 */
	public function getUserOrders($userId) {
		return $this->field(array('id'))->where(array(
			'user_id' => $userId,
			'status'	=> array('neq', 0),
		))
		->order('created desc')
		->limit($this->offset.','. $this->limit)
		->select();
	}
	
	public function reflushOrderList($userId, $firstOrderId) {
		return $this->field(array('id'))->where(array(
			'user_id' 	=> $userId,
			'status'	=> array('neq', 0),
			'id'		=> array('gt', $firstOrderId)
		))
		->order('id')
		->limit($this->limit)
		->select();
	}
	
	/**
	 * 获取美容师订单列表
	 * @param number $beauticianId	美容师编号
	 * @return boolean|array
	 */
	public function getBeauticianOrders($beauticianId) {
		$where = $this->getBeauticianMultiSelectWhere($beauticianId);
		
		return $this->field(array('id'))->where($where)
		->order($this->order)
		->limit($this->offset.','. $this->limit)
		->select();
	}
	
	/**
	 * 获取美容师已被预约的时间
	 * @param number $beauticianId	美容师编号
	 * @param number $todayStart	今天开始的时间戳
	 * @return boolean|array
	 */
	public function getBeauticianOrderTimes($beauticianId, $todayStart) {
		$unExpiresOrderSql = $this->field(array('order_time', 'service_time', 'created'))
			->where(array(
			'beautician_id' => $beauticianId,
			'status' => 2,
			'order_time' => array('egt', $todayStart),
			'expire_time' => array('gt', time() )
		))->buildSql();
		
		return $this->field(array('order_time', 'service_time','created'))->where(array(
			'beautician_id' => $beauticianId,
			'order_time' => array('egt', $todayStart),
			'status!=0 AND status!=2 AND status!=5'
		))
		->union($unExpiresOrderSql)
		->select();
	}
	
	/**
	 * 查询美容师正在服务订单
	 * @param number $beauticianId	美容师编号
	 * @return boolean|array
	 */
	public function getBeauticianCurrentService($beauticianId) {
		$where = array('beautician_id' => $beauticianId, 'status' =>3);
		
		return $this->where($where)->find();
	}
	
	/**
	 * 订单详情
	 * 
	 * @param number $id	订单编号
	 * @return boolean|array
	 */
	public function getOrderInfo($id) {
// 		$cacheKey = $this->getCacheKey($id);
// 		$orderInfo = S($cacheKey);
// 		if (!$orderInfo) {
			$orderInfo = $this->where(array('id' => $id))->find();
// 			S($cacheKey, $orderInfo);
// 		}
		
		return $orderInfo;
	}
	
	/**
	 * 统计美容师订单列表
	 * @param number $beauticianId	美容师编号
	 * @return number
	 */
	public function totalBeauticianOrder($beauticianId) {
		$where = $this->getBeauticianMultiSelectWhere($beauticianId);
		return $this->where($where)->count();
	}
	
	/**
	 * 统计用户订单
	 * @param number $userId	用户编号
	 * @return number
	 */
	public function totalUserOrder($userId) {
		return $this->where(array('user_id' => $userId, 'status!=0'))->count();
	}
	
	public function totalReflush($userId, $firstOrderId) {
		return $this->where(array('user_id' => $userId, 'status!=0', 'id' => array('gt', $firstOrderId)))->count();
	}
}